//
//  PicVersion.m
//  JianChaRiBao
//
//  Created by 常兴宇 on 13-10-30.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "PicVersion.h"
#import "UIImageView+WebCache.h"

@interface PicVersion ()<UIScrollViewDelegate>
{
    UIImageView * coverImageView;
    UILabel * coverLabel;
    
    UIScrollView *scaleScrollView;
    UIView *containView;
}

@end

@implementation PicVersion

-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        
        scaleScrollView = [[[UIScrollView alloc] initWithFrame:self.bounds] autorelease];
        scaleScrollView.backgroundColor = [UIColor clearColor];
        scaleScrollView.delegate = self;
        scaleScrollView.maximumZoomScale = 8;
        scaleScrollView.minimumZoomScale = 1;
        [self.contentView addSubview:scaleScrollView];
        
        containView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
        containView.backgroundColor = [UIColor clearColor];
        [scaleScrollView addSubview:containView];
        
        coverImageView = [[[UIImageView alloc] initWithFrame:self.bounds] autorelease];
        coverImageView.bounds = CGRectMake(0, 0, self.bounds.size.width * 0.8, self.bounds.size.height * 0.8);
        coverImageView.layer.shadowColor = [UIColor blackColor].CGColor;
        coverImageView.layer.shadowOpacity = 0.45;
        coverImageView.layer.shadowOffset = CGSizeMake(0, 6);
        coverImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:coverImageView.bounds].CGPath;
        coverImageView.layer.masksToBounds = NO;
//        coverImageView.contentMode = UIViewContentModeScaleAspectFit;
        [containView addSubview:coverImageView];

        
//        UIImageView * vorizontalImage = [[[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width - 5, 0, 5, self.bounds.size.height - 5)] autorelease];
//        vorizontalImage.image = [UIImage imageNamed:@"news_right.png"];
//        [self.contentView addSubview:vorizontalImage];
//        
//        UIImageView * horizontalImage = [[[UIImageView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - 5,self.bounds.size.width - 5,  5)] autorelease];
//        horizontalImage.image = [UIImage imageNamed:@"news_right.png"];
//        [self.contentView addSubview:horizontalImage];

        coverLabel = [[[UILabel alloc]initWithFrame:CGRectMake(coverImageView.frame.origin.x, coverImageView.frame.origin.y, 80, 20)] autorelease];
        coverLabel.backgroundColor = [UIColor blackColor];
        coverLabel.alpha = 0.7;
        coverLabel.textColor = [UIColor whiteColor];
        coverLabel.textAlignment = UITextAlignmentCenter;
        coverLabel.font = [UIFont systemFontOfSize:14.0f];
        [containView addSubview:coverLabel];
        
//        scaleScrollView.userInteractionEnabled = NO;
        
        
        UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap)] autorelease];
        doubleTap.numberOfTapsRequired = 2;
        [self addGestureRecognizer:doubleTap];
        
        UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)] autorelease];
        [tap requireGestureRecognizerToFail:doubleTap];
        coverImageView.userInteractionEnabled = YES;
        [coverImageView addGestureRecognizer:tap];
        
        
    }
    return self;
}
- (void)updateDisplayWithImageStr:(NSString *)str text:(NSInteger)text{
    NSString *coverURLStr = [[_baseInfo objectForKey:str] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [coverImageView setImageWithURL:[NSURL URLWithString:coverURLStr] placeholderImage:[UIImage imageNamed:@"loadingpic.png"] success:^(UIImage *image) {
        coverImageView.bounds = [self fitSizeForSize:image.size inSize:coverImageView.bounds.size];
        coverImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:coverImageView.bounds].CGPath;
        CGRect rect = coverLabel.frame;
        rect.origin = coverImageView.frame.origin;
        coverLabel.frame = rect;
        
    } failure:^(NSError *error) {
        nil;
    }];
    
    coverLabel.text = [NSString stringWithFormat:@"第%d版",text];


}

- (CGRect)fitSizeForSize:(CGSize)inSize inSize:(CGSize)outSize
{
    CGSize finalSize = CGSizeZero;
    
    if (inSize.width / inSize.height < outSize.width / outSize.height) {
        finalSize.height = outSize.height;
        finalSize.width = outSize.height / inSize.height * inSize.width;
    }
    else {
        finalSize.width = outSize.width;
        finalSize.height = outSize.width / inSize.width * inSize.height;
    }
    
    return CGRectMake(0, 0, finalSize.width, finalSize.height);
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}

- (void)tap
{
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"didSelectNewsCell" object:@(self.index)];
    [self.viewController showIndex:self.index];
}

- (void)doubleTap
{
    NSLog(@"doubleTap>>>>%f",scaleScrollView.zoomScale);
    [UIView animateWithDuration:0.3 animations:^{
        if (scaleScrollView.zoomScale <= 1) {
            scaleScrollView.zoomScale = 2;
        }
        else {
            scaleScrollView.zoomScale = 1;
        }
    }];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return containView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
